package com.jige.jigetodo.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jige.jigetodo.common.BaseResponse;
import com.jige.jigetodo.common.DeleteRequest;
import com.jige.jigetodo.common.ErrorCode;
import com.jige.jigetodo.common.ResultUtils;
import com.jige.jigetodo.exception.ThrowUtils;
import com.jige.jigetodo.model.dto.task.AddTaskRequest;
import com.jige.jigetodo.model.dto.task.QueryTaskRequest;
import com.jige.jigetodo.model.dto.task.UpdateTaskRequest;
import com.jige.jigetodo.model.entity.Task;
import com.jige.jigetodo.model.vo.TaskVO;
import com.jige.jigetodo.service.TaskService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;


    @PostMapping("/add")
    public BaseResponse<Boolean> taskAdd(@RequestBody AddTaskRequest addTaskRequest, HttpServletRequest request){
        ThrowUtils.throwIf(addTaskRequest==null, ErrorCode.PARAMS_ERROR);
        boolean result = taskService.addTask(addTaskRequest,request);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> taskUpdate(@RequestBody UpdateTaskRequest updateTaskRequest,HttpServletRequest request){
        ThrowUtils.throwIf(updateTaskRequest==null, ErrorCode.PARAMS_ERROR);
        boolean result = taskService.updateTask(updateTaskRequest, request);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/get")
    public BaseResponse<Task> taskGetTaskById(QueryTaskRequest queryTaskRequest){
        Task task = taskService.getTaskById(queryTaskRequest);
        ThrowUtils.throwIf(task==null,ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(task);
    }

    @PostMapping("/get/vo")
    public BaseResponse<TaskVO> taskGetTaskVOById(QueryTaskRequest queryTaskRequest){
        TaskVO taskVO = taskService.getTaskVOById(queryTaskRequest);
        ThrowUtils.throwIf(taskVO==null,ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(taskVO);
    }

    @PostMapping("/list")
    public BaseResponse<List<Task>> taskGetTaskList(QueryTaskRequest queryTaskRequest){
        List<Task> taskList = taskService.getTaskList(queryTaskRequest);
        if (CollUtil.isEmpty(taskList)){
            return ResultUtils.success(new ArrayList<>());
        }
        return ResultUtils.success(taskList);
    }

    @PostMapping("/list/vo")
    public BaseResponse<List<TaskVO>> taskGetTaskVOList(QueryTaskRequest queryTaskRequest){
        List<TaskVO> taskVOList = taskService.getTaskVOList(queryTaskRequest);
        if (CollUtil.isEmpty(taskVOList)){
            return ResultUtils.success(new ArrayList<>());
        }
        return ResultUtils.success(taskVOList);
    }

    @PostMapping("/page")
    public BaseResponse<Page<Task>> taskGetTaskPage(@RequestBody QueryTaskRequest queryTaskRequest){
        ThrowUtils.throwIf(queryTaskRequest==null,ErrorCode.PARAMS_ERROR);
        Page<Task> taskListByPage = taskService.getTaskListByPage(queryTaskRequest);
        return ResultUtils.success(taskListByPage);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> taskDeleteTaskById(@RequestBody DeleteRequest deleteRequest){
        ThrowUtils.throwIf(deleteRequest==null,ErrorCode.PARAMS_ERROR);
        boolean result = taskService.deleteTaskById(deleteRequest);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR,"删除失败，系统错误");
        return ResultUtils.success(true);
    }
}
